package com.txzt.gaea.business.project.controller;

import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.project.domain.dto.ProjectContractDTO;
import com.txzt.gaea.business.project.domain.model.ProjectContract;
import com.txzt.gaea.business.project.domain.vo.ProjectContractVo;
import com.txzt.gaea.business.project.service.ProjectContractService;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.page.PageInfoBT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/project/contract")
public class ProjectContractController extends BaseController {

    @Autowired
    private ProjectContractService projectContractService;

    @PostMapping(value = "/list")
    public RtnInfo<PageInfoBT<ProjectContractVo>> pageList(@RequestBody ProjectContractDTO projectContractDTO){
        return projectContractService.pageList(projectContractDTO);
    }

    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo add(@RequestBody ProjectContract projectContract){
        if (null != projectContract && null == projectContract.getId()){
            //查重
            if (!projectContractService.addCheck(projectContract)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查合同编号、合同名称");
            }
            int num = projectContractService.save(projectContract);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据添加失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/item/{id}")
    public RtnInfo getInfoById(@PathVariable Long id){
        return projectContractService.getInfo(id);
    }

    @PostMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo edit(@RequestBody ProjectContract projectContract){
        if (null != projectContract && null != projectContract.getId()){
            //查重
            if (!projectContractService.updateCheck(projectContract)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查合同编号、合同名称");
            }
            int num = projectContractService.update(projectContract);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据更新失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/delete/{id}")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo remove(@PathVariable Long id){
        return RtnInfo.success(projectContractService.delete(id));
    }
}
